package top.soulblack.purchasesale.controller.setting;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.soulblack.purchasesale.common.Result;
import top.soulblack.purchasesale.model.GoodsUnit;
import top.soulblack.purchasesale.service.GoodsUnitService;

import static top.soulblack.purchasesale.common.CodeMsg.*;

/**
 * @Author: 廉雪峰
 * @Date: 2019/5/19 21:00
 * @Version 1.0
 */
@Controller
@RequestMapping("/unit")
public class GoodsUnitController {

    @Autowired
    private GoodsUnitService goodsUnitService;

    @RequestMapping("/addUnit")
    @ResponseBody
    public Result<Boolean> addCorlor(GoodsUnit goodsUnit) {
        // 判断是否存在当前颜色
        if (goodsUnitService.existUnit(goodsUnit.getName())) {
            return Result.error(UNIT_EXIST);
        }
        // 判断排序数是否在1到100区间
        if (goodsUnit.getSort() > 100 || goodsUnit.getSort() < 1) {
            return Result.error(SORT_ERROR);
        }
        goodsUnitService.addUnit(goodsUnit);
        return Result.success(true);
    }

    @RequestMapping("/deleteUnit")
    @ResponseBody
    // 后期需要加上User权限验证，否则暴露接口有危险。
    public Result<Boolean> deleteUnit(int unitId) {
        if (goodsUnitService.deleteUnit(unitId)) {
            return Result.success(true);
        }
        return Result.error(UNIT_DELETE);
    }
    @RequestMapping("/editUnit")
    @ResponseBody
    public Result<Boolean> editUnit(GoodsUnit goodsUnit) {
        if (goodsUnitService.updateUnit(goodsUnit)) {
            return Result.success(true);
        }
        return Result.error(UNIT_EDIT_ERROR);
    }
}
